package com.atguigu.gulimall.product.utils;


import com.atguigu.gulimall.product.entity.CategoryEntity;

import java.util.ArrayList;
import java.util.List;

public class TreeUtils {
    public static List<CategoryEntity> buildCategoryEntityTree(List<CategoryEntity> menuList, Long rootId) {
        List<CategoryEntity> result = new ArrayList<>();
        for (CategoryEntity menus : menuList) {
            Long parentId = menus.getParentCid();
            if(parentId.equals(rootId)){
                Long id = menus.getCatId();
                List<CategoryEntity> children = buildCategoryEntityTree(menuList, id);
                if(children!=null&&children.size()>0)
                    menus.setChildren(children);
                result.add(menus);
            }
        }
        return result;
    }
}
